package top.infopub.mgr.hr.service.impl;

import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import top.infopub.generic.GenericDao;
import top.infopub.generic.GenericServiceImpl;
import top.infopub.mgr.hr.dao.PostInfoDao;
import top.infopub.mgr.hr.service.PostInfoService;
import top.infopub.model.hr.PostGroupInfo;
import top.infopub.model.hr.PostInfo;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.tdp.sys.service.CodeGeneratorConfigService;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;

@Service
public class PostInfoServiceImpl extends GenericServiceImpl<PostInfo, String> implements
		PostInfoService {

	@Autowired
	private PostInfoDao postInfoDao;

	@Autowired
	private CodeGeneratorConfigService codeGeneratorConfigService;

	@Override
	public GenericDao<PostInfo, String> getDao() {
		return postInfoDao;
	}

    @Override
    public List<PostInfo> getPostByDeptId(String deptId) {
        return postInfoDao.selectByDeptId(deptId);
    }

    @Override
    public void save(PostInfo postInfo) {
        postInfo.setUptBy(SessionUserUtils.getSessionUserId());
        postInfo.setUptTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
        if(StringUtils.isEmpty(postInfo.getId())){
            String postCode = codeGeneratorConfigService.generatorCode("hr_post_info");
            postInfo.setPostCode(postCode);
            postInfo.setPostName(postInfo.getPostName().trim());
            postInfo.setState(1);//默认可用
            postInfo.setAddBy(SessionUserUtils.getSessionUserId());
            postInfo.setAddTime(top.infopub.mgr.util.DateUtils.getCurrentDateTime());
            postInfoDao.insert(postInfo);
        }else{
            postInfoDao.updateByIdSelective(postInfo);
        }
    }

    @Override
    public Page<PostInfo> selectPageByDomainWhere(Page<PostInfo> page, PostInfo postInfo) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PostInfo>)postInfoDao.selectByDomainWhere(postInfo);
    }

    @Override
    public Page<PostInfo> selectPageByGroupId(Page<PostInfo> page, String groupId,String roleType) {
        PageHelper.startPage(page.getPageNum(), page.getPageSize());
        return (Page<PostInfo>)postInfoDao.selectByGroupId(groupId,roleType);
    }

    @Override
    public List<PostInfo> selectByPostNameAndId(String postName, String deptId, String id) {
        return postInfoDao.selectByPostNameAndId(postName,deptId,id);
    }

    @Override
    public List<PostInfo> selectPostByName(String postName, String type) {
        return postInfoDao.selectPostByName(postName,type);
    }

    @Override
    public List<PostGroupInfo> selectPostGroupInfo(String groupId, String roleType) {
        return postInfoDao.selectPostGroupInfo(groupId,roleType);
    }
}
